package com.medialog.am.core;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import com.medialog.am.vo.CollectInfo;
import com.medialog.am.vo.ProcessedData;

public class DataHandling{

	static Logger logger = Logger.getLogger(DataHandling.class);
	
	
	public DataHandling() {
	}
	
	public List<ProcessedData> dataProcessing(List<CollectInfo> collectInfoList){
		
		logger.debug("dataProcessing entry");	
		List<ProcessedData> processedDataList = new ArrayList<ProcessedData>();
		
		for(CollectInfo collectInfo : collectInfoList){
			ProcessedData processedData =  new ProcessedData();
			processedData.setSvrNum(collectInfo.getSvrNum());
			processedData.setSerialNo(collectInfo.getSerialNo());
			
			processedData.setCpuRate(Float.parseFloat(collectInfo.getUsedCpu()));
			processedData.setMemRate(Float.parseFloat(collectInfo.getUsedMem())/Float.parseFloat(collectInfo.getTotalMem())*100);
			processedData.setHddRate(Float.parseFloat(collectInfo.getUsedHdd())/Float.parseFloat(collectInfo.getTotalHdd())*100);
	
			String alarmCpu = criticalPoint(collectInfo.getCpuCritical(), collectInfo.getCpuMajor(), collectInfo.getCpuMinor(), processedData.getCpuRate());
			String alarmMem = criticalPoint(collectInfo.getMemCritical(), collectInfo.getMemMajor(), collectInfo.getMemMinor(), processedData.getMemRate());
			String alarmHdd = criticalPoint(collectInfo.getHddCritical(), collectInfo.getHddMajor(), collectInfo.getHddMinor(), processedData.getHddRate());
			processedData.setAlarmCpu(alarmCpu);
			processedData.setAlarmMem(alarmMem);
			processedData.setAlarmHdd(alarmHdd);
			
			processedDataList.add(processedData);
			logger.debug("Processed Data "+"CPU RATE: "+processedData.getCpuRate()+" MEM RATE: "+processedData.getMemRate()+" HDD RATE: "+processedData.getHddRate());
		}
		logger.debug("DataProcessing EXIT");	
		return processedDataList;
	}



	public String criticalPoint(int critical, int major, int minor, float svrPoint){
	
		if(svrPoint >= critical)
			return "CRITICAL";
		else if(svrPoint < critical && svrPoint >=major)
			return "MAJOR";
		else if(svrPoint < major && svrPoint >= minor)
			return "MINOR";
		else return "NORMAL";
	}
}